<include file="Public/head"/>
<body class="no-skin">
<include file="Public/header"/>
<div class="main-container" id="main-container">
    <script type="text/javascript">
        try {
            ace.settings.check('main-container', 'fixed')
        } catch (e) {
        }
    </script>
    <include file="Public/sidebar"/>
        <div class="main-content">
            <div class="main-content-inner">
            <!-- #section:basics/content.breadcrumbs -->
                <include file="Public/breadcrumbs"/>
                <!-- /section:basics/content.breadcrumbs -->
                    <div class="page-content">
                        <include file="Public/set"/>

                        <!-- /section:settings.box -->
                        <div class="row">
                            <div class="col-xs-12 "  style="padding: 35px;" >
                                <div class="col-xs-8 col-xs-offset-2">
                                    <div class="row text-right">
                                        <button class="btn btn-success" data-toggle="modal" data-target=".o-sm">
                                            添加
                                        </button>
                                    </div>
                                    <div class="row">
                                        <table class="table table-bordered table-hover">
                                            <tr>
                                                <TH class="text-center">单位</TH>
                                                <th class="text-right">操作</th>
                                            </tr>
                                            <volist name="info" id="vo">
                                                <tr class="un_{$vo['unitid']}">
                                                    <td class="text-center">{$vo['unitname']}</td>
                                                    <td  class="text-right">
                                                        <button class="btn btn-primary" onclick="uni_change({$vo['unitid']})">修改</button>
                                                        <button class="btn btn-danger" onclick="uni_delete({$vo['unitid']})">删除</button>
                                                    </td>
                                                </tr>
                                            </volist>
                                        </table>
                                    </div> 
                                </div>
                                <div class="row text-center">
                                    {$page}
                                </div>
                            </div><!-- /.col -->
                        </div><!-- /.row -->
                    </div><!-- /.page-content -->
                </div>
            </div><!-- /.main-content -->
        <include file="Public/footer"/>

        </div><!-- /.main-container -->

        <include file="Public/footerjs"/>
    <!-- inline scripts related to this page -->
    </body>


<div class="modal fade o-sm" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
  <div class="modal-dialog modal-sm" role="document">
    <div class="modal-content">
        <div class="modal-header">
            <h3 class="modal-title">
                 添加单位
            </h3>
        </div>
        <div class="modal-body">
            <input type="text" name="dw" class="dw form-control">
        </div>
        <div class="modal-footer">
            <button  class="btn btn-success btn-add" >确定</button>
            <button type="button" class="btn btn-default " data-dismiss="modal">关闭</button>
        </div>
    </div>
  </div>
</div>

<div class="modal fade b-sm" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
  <div class="modal-dialog modal-sm" role="document">
    <div class="modal-content">
        <div class="modal-header">
            <h3 class="modal-title">
                修改单位
            </h3>
        </div>
        <div class="modal-body">
            <input type="hidden" name="id">
            <input type="text" name="name" class="dw form-control">
        </div>
        <div class="modal-footer">
            <button  class="btn btn-success btn-cng" >确定</button>
            <button type="button" class="btn btn-default " data-dismiss="modal">关闭</button>
        </div>
    </div>
  </div>
</div>



</html>
<script type="text/javascript">
    function uni_change(id) {
        $(".b-sm").modal('show');
        $.ajax({
            url:'{:U('Unitsofmeasure/get_id')}?id='+id,
            success:function(data){
                console.log(data);
                if(data.status){
                    $("input[name=name]").val(data.con.unitname);
                    $("input[name=id]").val(id);
                }else{

                }
            }
        })
    }
    $(".btn-cng").click(function(){
        var id=$("input[name=id]").val();
        var name=$("input[name=name]").val();
        $.ajax({
            url:'{:U('Unitsofmeasure/change_one')}',
            data:{
                id:id,
                name:name
            },
            method:'post',
            success:function(data){
                if(data.status){
                    $(".un_"+id+" td:eq(0)").html(data.con.name);
                     $(".b-sm").modal('hide');
                }else{
                    confirm("操作失败:"+data.con);
                }
            }
        })
    })
    $(".btn-add").click(function(){
        $(".modal-title").val('添加单位');
        var addname=$(".dw").val();
        if(addname){
            $.ajax({
                url:'{:U('Unitsofmeasure/add_one')}?name='+addname,
                success:function(data){
                    if(data.status){
                        var tr=$("<tr class='un_"+data.con.id+"'></tr>");
                        var td_1=$("<td class='text-center'>"+data.con.name+"</td>");
                        var td_2=$("<td class='text-right'><button class='btn btn-primary' onclick='uni_change("+data.con.id+")'>修改</button> <button class='btn btn-danger' onclick='uni_delete("+data.con.id+")'>删除</button></td>");
                        td_1.appendTo(tr);
                        td_2.appendTo(tr);
                        tr.appendTo(".table-hover");
                        $(".o-sm").modal('hide');
                        $(".dw").val("");
                    }else{
                        confirm("添加失败:"+data.con);
                    }
                }
            })
        }
    })

    function uni_delete(id){
        jugg=confirm("确定要删除该");
        if(jugg){
            $.ajax({
                url:'{:U('Unitsofmeasure/delete_one')}?id='+id,
                success:function(data){
                    if(data.status){
                        $(".un_"+id).remove();
                    }else{
                        confirm("删除失败"+data.con);
                    }
                }
            })
        }
    }
</script>